<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CtiCdrWav;
use app\models\search\CtiCdrWavSearch;
use yii\helpers\ArrayHelper;

class CtiManagerLyController  extends CtiManagerController
{

    /**
     * 文件列表
     */
    public function actionFileList()
    {
        $params['controller'] = $this->https_url."uploads/download";
        return $this->getFileList(new CtiCdrWav(),null,$params);
    }

    /**
     * 文件列表
     */
    public function actionSearchFileList()
    {
        $search = new CtiCdrWavSearch();
        $res = $search->search(\Yii::$app->request->post());
        $count = $res->query->count();
        $res->query->limit($_POST['limit']);
        $res->query->offset(($_POST['page']-1)*$_POST['limit']);
        $final = $res->query->asArray()->all();
        return $this->actionLayData($count,$final);
    }

    /**
     * 查询
     */
    public function actionFind($id)
    {
        $model = new CtiCdrWav();
        $res = ArrayHelper::toArray($model->find()->where(['uuid'=>$id])->one());
        return json_encode($res);
    }

    /**
     * 扫描所有的文件
     * @param $path
     * @return array|mixed|string
     */
    function scanFile($path) {
        global $result;
        $files = scandir($path);
        foreach ($files as $k=>$file) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . '/' . $file)) {
                    $this->scanFile($path . '/' . $file);
                } else {
                    $result[$k] = pathinfo($path . '/' . $file);
                    strpos($path . '/' . $file,$this->remark->recordPath);
                    $result[$k]['httppath'] = $this->https_url."uploads/freeswitch".substr($path . '/' . $file,strlen($this->remark->recordPath),strlen($path . '/' . $file)-strlen($this->remark->recordPath));;
                }
            }
        }
        return $result;
    }


}